/*
 * Created: 2022-08-25 13:11:30
 * Author : 涛涛涛
 */

import React, { useCallback, useContext, FC, useMemo } from 'react';
import { View, Text, ScrollView, ScrollViewProps, BaseEventOrig } from '@tarojs/components';
import { styled } from 'linaria/react';
import { useClassName } from '@/utils/index';
import { HomeContext } from '../RootPageComponentContext';
type LocalCompProps = Omit<ScrollViewProps, 'style'> & ICompProps;
export interface IProps extends LocalCompProps {
    /** 中间核心主要区域 */
    children: React.ReactElement | React.ReactElement[] | ((renderProps: { height: number }) => React.ReactElement);
}
MyScrollBody.defaultProps = {} as Partial<IProps>;
function MyScrollBody(props: IProps) {
    const { className, style, children, ...others } = props;
    const localClassName = useClassName('MyScrollBody', className);
    const {
        enableExpand,
        scrollOffsetBottom,
        headFixedSlot,
        rootLayoutState: { headNavHeight, bodyScrollHeight, headFixedName, bodyScrollChildHeight },
        rootLayoutDispatch,
    } = useContext(HomeContext);
    /** 盒子滚动事件 */
    const handleBodyScroll = useCallback(
        (event: BaseEventOrig<ScrollViewProps.onScrollDetail>) => {
            //如果允许折叠,则会调用该dispatch重新计算头部导航的高度
            if (enableExpand) {
                rootLayoutDispatch({
                    type: 'figureHeadNavHeightOnBodyScroll',
                    payload: { event },
                });
            }
        },
        [rootLayoutDispatch, enableExpand]
    );
    const localChildren = useMemo(() => {
        if (typeof children === 'function') {
            return children({ height: bodyScrollChildHeight });
        }
        return children;
    }, [bodyScrollChildHeight, children]);
    return (
        <RootScrollView
            {...others}
            className={localClassName}
            style={{ ...style, height: bodyScrollHeight + 'px' }}
            scrollY={true}
            onScroll={handleBodyScroll}>
            <View style={{ height: headNavHeight + 'px' }}></View>
            <View className={headFixedName}>{headFixedSlot}</View>
            {localChildren}
            <View style={{ height: scrollOffsetBottom + 'px' }}></View>
        </RootScrollView>
    );
}
export default React.memo(MyScrollBody);
const RootScrollView = styled(ScrollView)`
    &.MyScrollBody {
    }
`;
